{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 理解 NumPy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "NumPy 是一个功能强大的 Python 库，允许更高级的数据操作和数学计算\n",
    "\n",
    "### 什么是 NumPy？\n",
    "NumPy 提供了大量的库函数和操作，用于进行数值计算，广泛应用于以下任务：\n",
    "- **机器学习模型：**在编写机器学习算法时，需要对矩阵进行各种数值计算。例如矩阵乘法、换位、加法等。NumPy提供了一个非常好的库，用于简单(在编写代码方面)和快速(在速度方面)计算。NumPy数组用于存储训练数据和机器学习模型的参数。\n",
    "\n",
    "- **图像处理和计算机图形学：**计算机中的图像表示为多维数字数组。NumPy成为同样情况下最自然的选择。实际上，NumPy提供了一些优秀的库函数来快速处理图像。例如，镜像图像、按特定角度旋转图像等。\n",
    "\n",
    "- **数学任务：**NumPy对于执行各种数学任务非常有用，如数值积分、微分、内插、外推等。因此，当涉及到数学任务时，它形成了一种基于Python的MATLAB的快速替代。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### NumPy 的安装\n",
    "通过 shell 命令：\n",
    "``` Python\n",
    "pip install numpy \n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### NumPy 中的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n"
     ]
    }
   ],
   "source": [
    "# 快速定义一维 NumpPy 数组\n",
    "import numpy as np\n",
    "my_array = np.array([1, 2, 3, 4, 5])\n",
    "print(my_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(5,)\n"
     ]
    }
   ],
   "source": [
    "# 打印我们创建的数组的形状 (行, 列)\n",
    "print(my_array.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "[-1  2  3  4  5]\n"
     ]
    }
   ],
   "source": [
    "# 打印各个元素，类似于普通的 python 数组，起始索引编号为 0\n",
    "print(my_array[0])\n",
    "print(my_array[1])\n",
    "my_array[0] = -1\n",
    "print(my_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0. 0. 0. 0. 0.]\n",
      "[0. 0. 0. 0. 0.]\n",
      "[1. 1. 1. 1. 1.]\n"
     ]
    }
   ],
   "source": [
    "# 快速创建一个长度为 5 的 NumPy 数组，所有元素都为 0\n",
    "my_new_array = np.zeros((5))\n",
    "print(my_new_array)\n",
    "## 类似的还有 np.zeros，np.ones\n",
    "print(np.zeros((5)))\n",
    "print(np.ones((5)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.15667542 0.78864447 0.38071132 0.00993177 0.86048142]\n"
     ]
    }
   ],
   "source": [
    "# 创建一个随机值数组可以使用 random，它将为每个元素分配 0 到 1 之间的随机值\n",
    "my_random_array = np.random.random((5))\n",
    "print(my_random_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "(2, 2)\n",
      "[[4 5]\n",
      " [6 1]]\n"
     ]
    }
   ],
   "source": [
    "# 指定值的数组\n",
    "my_array = np.array([[4, 5], [6, 1]])\n",
    "print(my_array[0][1])\n",
    "print(my_array.shape)\n",
    "print(my_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 1]\n"
     ]
    }
   ],
   "source": [
    "# 想要提取第二列（索引 1）的所有元素\n",
    "my_array_column_2 = my_array[:, 1]\n",
    "print(my_array_column_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### NumPy 中的数组操作\n",
    "使用 NumPy 我们可以方便地在数组上执行数学运算。例如，可以添加，减去 NumPy 数组，也可以减去它们，将它们相乘，或者将它们分开。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sum = \n",
      " [[ 6.  8.]\n",
      " [10. 12.]]\n",
      "Difference = \n",
      " [[-4. -4.]\n",
      " [-4. -4.]]\n",
      "Product = \n",
      " [[ 5. 12.]\n",
      " [21. 32.]]\n",
      "Quotient = \n",
      " [[0.2        0.33333333]\n",
      " [0.42857143 0.5       ]]\n",
      "Matrix Product = \n",
      " [[19. 22.]\n",
      " [43. 50.]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "a = np.array([[1.0, 2.0], [3.0, 4.0]])\n",
    "b = np.array([[5.0, 6.0], [7.0, 8.0]])\n",
    "sum = a + b\n",
    "difference = a - b\n",
    "product = a * b\n",
    "quotient = a / b\n",
    "print(\"Sum = \\n\", sum)\n",
    "print(\"Difference = \\n\", difference)\n",
    "print(\"Product = \\n\", product)\n",
    "print(\"Quotient = \\n\", quotient)\n",
    "\n",
    "# 上面的乘法运算发执行的是逐元素乘法，如果要执行矩阵乘法，可以执行以下操作\n",
    "matrix_product = a.dot(b)\n",
    "print(\"Matrix Product = \\n\", matrix_product)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 本章结束"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
